import { parse } from "qs";
import { message, notification } from "antd";
import moment from "moment";
import { isEmpty, isArray, cloneDeep, merge } from "lodash";
import { hashHistory } from "dva/router";
import {
  regionArea,
  getProduct,
  dowmfa,
  down,
} from "./commissionInvoiceListServ.js";
import { makeAddress, download } from "utils/util";
import config from "config/config";
function downloadFile(data, type, fileName) {
    let blob = new Blob([data], {type: type});
    // 获取heads中的filename文件名
    let downloadElement = document.createElement('a');
    // 创建下载的链接
    let href = window.URL.createObjectURL(blob);
    downloadElement.href = href;
    // 下载后文件名
    downloadElement.download = fileName;
    document.body.appendChild(downloadElement);
    // 点击下载
    downloadElement.click();
    // 下载完成移除元素
    document.body.removeChild(downloadElement);
    // 释放掉blob对象
    window.URL.revokeObjectURL(href);
}
const defultState = {
  biz: {},
  area1: [],
  products: [],
  // 核销状态
  writeOffList: [
    {
      value: 0,
      label: "未核销",
    },
    {
      value: 1,
      label: "已核销",
    },
  ],
  // 服务费结算状态
  taxServiceStatusList: [
    {
      value: -1,
      label: "结算异常",
    },
    {
      value: 0,
      label: "未结算",
    },
    {
      value: 1,
      label: "已结算",
    },
  ],
  // 同步网盈状态
  taxSyncStatusList: [
    {
      value: 0,
      label: "未同步",
    },
    {
      value: 1,
      label: "已完成",
    },
    {
      value: 2,
      label: "处理中",
    },
    {
      value: 3,
      label: "处理失败",
    },
    {
      value: 4,
      label: "已开票",
    },
  ],
  downDisable: [],
};

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        if ("" + tmpModule.pathname === "" + pathname) {
          dispatch({ type: "regionArea", payload: {} });
          dispatch({ type: "updateStore", payload: cloneDeep(defultState) });
        }
      });
    },
  },

  effects: {
    *regionArea({ payload }, { put, call, select }) {
      let { biz, areaList } = yield select(
        (state) => state[tmpModule.namespace]
      );
      const { data, success } = yield call(regionArea);
      if (success) {
        if (biz.queryForm && biz.queryForm.regionId) {
          data.map((item, index) => {
            if (
              biz.queryForm &&
              biz.queryForm.regionId &&
              biz.queryForm.regionId == item.value
            ) {
              areaList = item.children;
            }
          });
        }
        yield put({ type: "updateStore", payload: { area1: data, areaList } });
      }
    },
    *getProduct({ payload }, { put, call, select }) {
      let { biz, products } = yield select(
        (state) => state[tmpModule.namespace]
      );
      const { data, success } = yield call(getProduct, payload);
      if (success) {
        products = data;
        yield put({ type: "updateStore", payload: { products } });
      }
    },
    // 导出表格
    *down({ payload }, { put, call, select }) {
      try {
        let tmpState = yield select((state) => state[tmpModule.namespace]);
        let { queryForm } = tmpState.biz;
        const { data, success } = yield call(down, queryForm);
        yield put({ type: "updateStore", payload: { exportLoading: false } });
        if (success) {
          // yield put({ type: 'updateStore', payload: {  data } });
          download(data);
          message.success("操作成功");
          // window.open(data.url)
        }
      } catch (e) {
        console.error(e);
      }
    },
    // 下载发票
    *dowmfa({ payload }, { put, call, select }) {
      try {
        let href = `${config.baseUrl}3rd/commissionInvoice/downloadCommissionInvoice/${payload}`;
        let { data } = yield call(dowmfa, payload);
        let { msg } = data;
        let tmpState = yield select((state) => state[tmpModule.namespace]);
        let { downDisable } = tmpState;

        if (!msg) {
            // downloadFile(data,'application/image','gbgg')
            download(href) //原来a便签下载
        } else {
          notification["error"]({
            duration:2,
            message: "下载失败",
            description:msg,
          });
        }
        for (let i = 0; i < downDisable.length; i++) {
          downDisable[i].isclikdown = false;
        }
        yield put({ type: "updateStore", payload: { downDisable } });
      } catch (e) {
        console.error(e);
        let tmpState = yield select((state) => state[tmpModule.namespace]);
        let { downDisable } = tmpState;
        for (let i = 0; i < downDisable.length; i++) {
          downDisable[i].isclikdown = false;
        }
        yield put({ type: "updateStore", payload: { downDisable } });
      }
    },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload);
    },
  },
};

export default tmpModule;
